package com.cgbl.service.vm

import android.app.Application
import androidx.databinding.ObservableField
import api.WorkServiceFactory
import com.cgbl.service.assist.ProjectAssist
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.BaseResponse
import com.robot.base.model.MemberAdapterBean
import com.robot.base.util.ActivityUtils
import com.robot.base.util.LiveDataEventManager
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent

class MemberFrameProfileViewModel(application: Application) : GLBaseViewModel(application) {

    private lateinit var member: MemberAdapterBean

    val memberNameField = ObservableField<String>()

    val memberStatusField = ObservableField<String>()

    val memberJobField = ObservableField<String>()

    val memberRewardField = ObservableField<String>()

    val clickSetRewardCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityResult(
            AppConstants.Router.Project.A_SET_REWARD,
            ActivityUtils.getTopActivity(),
            AppConstants.ActivityRequestCode.SET_WARD_RESULT
        )
    })


    val clickSetJobCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityResult(
            AppConstants.Router.Project.A_SET_JOB,
            ActivityUtils.getTopActivity(),
            AppConstants.ActivityRequestCode.SET_JOB_RESULT
        )
    })

    val clickSelectJobStatusCommand = BindingCommand<Any>(BindingAction {
        uc.showSelectJobStatusEvent.call()
    })

    val clickDeleteProfileCommand = BindingCommand<Any>(BindingAction {
        uc.showDeleteProfileEvent.call()
    })

    val clickSaveProfileCommand = BindingCommand<Any>(BindingAction {
        insert()
    })

    fun setMember(member: MemberAdapterBean) {
        this.member = member
        memberNameField.set(member.nickName)
        memberRewardField.set("￥" + member.wages)
        memberJobField.set(member.career)
        memberStatusField.set(if (member.onlineStatus == "0") "在职" else "离职")
    }

    private fun insert() {
        val projectId = ProjectAssist.getCurrentProjectId()
        val params = mapOf(
            "itemAccountId" to member.itemAccountId,
            "accountId" to member.accountId,
            "career" to member.career,
            "itemInfoId" to projectId,
            "wages" to member.wages,
            "onlineStatus" to member.onlineStatus
        )
        WorkServiceFactory.createProjectAccount(params)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
                    LiveDataEventManager.postRefreshProjectProfileEvent()
                    finish()
                }
            })
    }

    fun delete() {
        WorkServiceFactory.deleteProjectAccount(member.id)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
                    LiveDataEventManager.postRefreshProjectProfileEvent()
                    finish()
                }
            })
    }

    val uc = UIChangeEvent()

    class UIChangeEvent {
        val showSelectJobStatusEvent = SingleLiveEvent<Void>()

        val showDeleteProfileEvent = SingleLiveEvent<Void>()

    }
}